package kr.hyunist.androidcomplete01saeildamil.ch17;

import java.util.*;

import kr.hyunist.androidcomplete01saeildamil.*;
import kr.hyunist.androidcomplete01saeildamil.ch19.*;
import android.app.*;
import android.content.*;
import android.os.*;
import android.view.*;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.*;

public class ActivityIfEx extends Activity {
	ArrayList<ActivityInfo> arList;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.listviewtest);
		
		arList = new ArrayList<ActivityInfo>();
		ActivityInfo info;
		info = new ActivityInfo(ThreadEx01.class, "ThreadEx01");
		arList.add(info);
		info = new ActivityInfo(ThreadEx02.class, "ThreadEx02");
		arList.add(info);
		
		MyListAdapter adpater = new MyListAdapter(this, arList);
		
		ListView list = (ListView) findViewById(R.id.listViewTestlist);
		list.setAdapter(adpater);
		
		list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Intent intent = new Intent(ActivityIfEx.this, arList.get(arg2).cls);
				startActivity(intent);
			}
		});
	}
}

class ActivityInfo {
	Class<?> cls;
	String name;

	public ActivityInfo(Class<?> cls, String name) {
		this.cls = cls;
		this.name = name;
	}

}

class MyListAdapter extends BaseAdapter {
	ArrayList<ActivityInfo> mInfo;
	Context mContext;
	LayoutInflater inflater;
	
	public MyListAdapter(Context context, ArrayList<ActivityInfo> arList) {
		mContext = context;
		mInfo = arList;
		inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		return mInfo.size();
	}

	@Override
	public Object getItem(int arg0) {
		return mInfo.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		if(arg1 == null){
			arg1 = inflater.inflate(R.layout.clslist_item, arg2, false);
		}
		
		TextView text = (TextView) arg1.findViewById(R.id.clslist_itemtxt);
		text.setText(mInfo.get(arg0).name);
		
		return arg1;
	}
	
}